#include "jeux.h"
void jouer (SDL_Surface *screen,char chemin[],char personnage,char milieu)
{   int tempsActuel,tempsPrecedant;
    int nbre_seconde=120;
    TTF_Init();
    char T[30]="";
    SDL_Surface *vide=NULL,*haut=NULL,*gauche=NULL,*bas=NULL,*droite=NULL,*pion=NULL;
    SDL_Rect pos_blit,pos_pion,pos_resultat,mine_bas,mine_haut,mine_gauche,mine_droite;
    SDL_Surface *resultat_gagnant=NULL, *resultat_perdant=NULL;
   //SDL_Surface *screen_resultat = NULL;
   //creer une nouvelle surface (chrono)
    TTF_Font *fontTest;
    fontTest=TTF_OpenFont("crimson_text_roman.ttf",22);
    SDL_Color colorChrono={0,0,0};
    //sprintf(T, "%d", nbre_seconde);
    //tempsActuel=SDL_GetTicks();
    SDL_Surface *chrono=NULL;
    //chrono=TTF_RenderText_Blended(fontTest,T,colorChrono);*/
    SDL_Rect pos_chrono;
    pos_chrono.x=220;
    pos_chrono.y=250;

    SDL_Event event;


    SDL_Rect pos_menu;
    pos_menu.x=500;
    pos_menu.y=0;
    int i,j,x,y;
    int grille [nbre_case][nbre_case]={0};

   //chargement des images
   if (milieu == 'f')
   {
        vide=SDL_LoadBMP("image/milieu/forestier/vide.bmp");
        haut=SDL_LoadBMP("image/milieu/forestier/haut.bmp");
        gauche=SDL_LoadBMP("image/milieu/forestier/gauche.bmp");
        bas=SDL_LoadBMP("image/milieu/forestier/bas.bmp");
        droite=SDL_LoadBMP("image/milieu/forestier/droite.bmp");
   }

    else if(milieu == 'u')
       {
            vide=SDL_LoadBMP("image/milieu/urbain/vide.bmp");
            haut=SDL_LoadBMP("image/milieu/urbain/haut.bmp");
            gauche=SDL_LoadBMP("image/milieu/urbain/gauche.bmp");
            bas=SDL_LoadBMP("image/milieu/urbain/bas.bmp");
            droite=SDL_LoadBMP("image/milieu/urbain/droite.bmp");
       }

   //else printf ("choisissez le type de labyrinthe");
   if (personnage== 'r')
   {
        pion=SDL_LoadBMP("image/personnage/rouge.bmp");
   }
        else if (personnage == 'b')
            pion=SDL_LoadBMP("image/personnage/bleu.bmp");

   SDL_SetColorKey(pion,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,255,255,255));

    resultat_gagnant=IMG_Load("image/resultat/resultat_gagnant.jpg");
    resultat_perdant=IMG_Load("image/resultat/resultat_perdant.jpg");
   //chargement du pion et transparence
   //pion=SDL_LoadBMP("image/pion.bmp");


    SDL_Surface *menu_droite = NULL;
    menu_droite = IMG_Load("image/menu/menu_droite.jpg");
    if (menu_droite == NULL)
    {
        printf("incapable de charger l'image : %s\n",SDL_GetError());
    }
    x=-1;
    y=-1;

   if (!charger_niveau(chemin,grille,&x,&y)) exit(EXIT_FAILURE);

   for(i=0;i<nbre_case;i++)
    for(j=0;j<nbre_case;j++)
    {
        if (grille[i][j]>=5)
        {
            pos_pion.x=i;
            pos_pion.y=j;
            grille[i][j]=(grille[i][j])-5;
        }
    }


    int done=1;
    while(done)
    {
        SDL_WaitEvent(&event);
        switch(event.type)
        {
            case SDL_QUIT: done=0 ;break;
            //case SDL_MOUSEBUTTONDOWN: done=0;break;
            case SDL_KEYDOWN:
                switch (event.key.keysym.sym)
                {
                    case SDLK_ESCAPE: done=0; break;

                    //case SDLK_q: done=0; break;
                    case SDLK_s: sauvegarger_niveau(grille,pos_pion.x,pos_pion.y);done=0;break;
                    case SDLK_UP: deplacer_pion(grille,&pos_pion,HAUT); break;
                    case SDLK_LEFT: deplacer_pion(grille,&pos_pion,GAUCHE); break;
                    case SDLK_DOWN: deplacer_pion(grille,&pos_pion,BAS); break;
                    case SDLK_RIGHT: deplacer_pion(grille,&pos_pion,DROITE); break;
                }
                break;
        }
        //SDL_SetColorKey(pion,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,255,255,255));

        tempsActuel = SDL_GetTicks();
        if(tempsActuel-tempsPrecedant >= 1000){
                nbre_seconde--;
                sprintf(T, "%ld", nbre_seconde);

                chrono = TTF_RenderText_Blended(fontTest,T,colorChrono);
                SDL_FreeSurface(chrono);
                tempsPrecedant = tempsActuel;
            }

        SDL_FillRect(screen,NULL,SDL_MapRGB(screen->format,255,255,255));

        //blitage des surfaces selon la grille
        for(i=0;i<nbre_case;i++)
         for(j=0;j<nbre_case;j++)
         {
             pos_blit.x=i*taille_case;
             pos_blit.y=j*taille_case;
             switch(grille[i][j])
             {
                 case VIDE:    SDL_BlitSurface(vide,NULL,screen,&pos_blit);   break;
                 case HAUT:    SDL_BlitSurface(haut,NULL,screen,&pos_blit);   break;
                 case GAUCHE:  SDL_BlitSurface(gauche,NULL,screen,&pos_blit); break;
                 case BAS:     SDL_BlitSurface(bas,NULL,screen,&pos_blit);    break;
                 case DROITE:  SDL_BlitSurface(droite,NULL,screen,&pos_blit); break;
                 case -1:
                     {
                         SDL_BlitSurface(haut,NULL,screen,&pos_blit);
                         mine_haut.x=i;
                         mine_haut.y=j;
                     }break;
                 case -2:
                    {
                         SDL_BlitSurface(gauche,NULL,screen,&pos_blit);
                         mine_gauche.x=i;
                         mine_gauche.h=j;
                    } break;
                 case -3:
                    {
                         SDL_BlitSurface(bas,NULL,screen,&pos_blit);
                         mine_bas.x=i;
                         mine_bas.y=j;
                    }break;
                 case -4:
                    {
                        SDL_BlitSurface(droite,NULL,screen,&pos_blit);
                        mine_droite.x=j;
                        mine_droite.y=i;
                    }break;
                 /*case VIDE_PION:    SDL_BlitSurface(vide,NULL,screen,&pos_blit);  break;
                 case HAUT_PION:    SDL_BlitSurface(haut,NULL,screen,&pos_blit);   break;
                 case GAUCHE_PION:  SDL_BlitSurface(gauche,NULL,screen,&pos_blit); break;
                 case BAS_PION:     SDL_BlitSurface(bas,NULL,screen,&pos_blit);    break;
                 case DROITE_PION:  SDL_BlitSurface(droite,NULL,screen,&pos_blit); break;*/

             }
         }
    //blitage du pion
    pos_blit.x=pos_pion.x * taille_case;
    pos_blit.y=pos_pion.y * taille_case;
    SDL_BlitSurface(pion,NULL,screen,&pos_blit);
    SDL_BlitSurface(chrono,NULL,screen,&pos_chrono);
    if ((pos_pion.x*taille_case) == largeur_fenetre-taille_case && (pos_pion.y*taille_case) == hauteur_fenetre-taille_case)
    {
        pos_resultat.x=0;
        pos_resultat.y=0;
        menu_droite=NULL;
        //SDL_FreeSurface(fond);
        SDL_BlitSurface (resultat_gagnant,NULL,screen,&pos_resultat);
        //SDL_Init(SDL_INIT_VIDEO);
        //screen_resultat = SDL_SetVideoMode(screen_resultat_largeur,screen_resultat_hauteur,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
       // SDL_FillRect(screen_resultat,NULL,SDL_MapRGB(screen->format,255,255,255));
    }
    if (pos_pion.x == mine_haut.x && pos_pion.y==mine_haut.y)
    {
        menu_droite=NULL;
        SDL_BlitSurface (resultat_perdant,NULL,screen,&pos_resultat);
    }
    if (pos_pion.x == mine_bas.x&& pos_pion.y==mine_bas.y)
    {
        menu_droite=NULL;
        SDL_BlitSurface (resultat_perdant,NULL,screen,&pos_resultat);
    }
    if (pos_pion.x == mine_gauche.x && pos_pion.y==mine_gauche.y)
    {
        menu_droite=NULL;
        SDL_BlitSurface (resultat_perdant,NULL,screen,&pos_resultat);
    }
    if (pos_pion.x == mine_droite.x && pos_pion.y==mine_droite.y)
    {
        menu_droite=NULL;
        SDL_BlitSurface (resultat_perdant,NULL,screen,&pos_resultat);
    }
    SDL_BlitSurface(menu_droite,NULL,screen,&pos_menu);

    //actualise l'ecran
    SDL_Flip(screen);
    }

    //fprintf(stdout,"%d\n",SDL_GetTicks());
    //liberation des surfaces
    SDL_FreeSurface(vide);
    SDL_FreeSurface(haut);
    SDL_FreeSurface(gauche);
    SDL_FreeSurface(bas);
    SDL_FreeSurface(droite);
    SDL_FreeSurface(pion);
    TTF_CloseFont(fontTest);
    TTF_Quit();
    //SDL_Quit();
}
void deplacer_pion (int grille[][nbre_case],SDL_Rect *pos_pion,int direction)
{
    switch(direction)
    {
        case DROITE:
            if(pos_pion->x+1>=nbre_case) break;
            if(grille[pos_pion->x+1][pos_pion->y]==GAUCHE || grille[pos_pion->x][pos_pion->y]==DROITE) break;
            pos_pion->x++;
            break;
        case GAUCHE:
            if(pos_pion->x-1<0) break;
            if(grille [pos_pion->x-1][pos_pion->y]==DROITE ||grille[pos_pion->x][pos_pion->y]==GAUCHE) break;
            pos_pion->x--;
            break;
        case BAS:
            if(pos_pion->y+1>=nbre_case) break;
            if(grille[pos_pion->x][pos_pion->y+1]==HAUT || grille[pos_pion->x][pos_pion->y]==BAS)break;
            pos_pion->y++;
            break;
        case HAUT:
            if(pos_pion->y-1<0) break;
            if(grille[pos_pion->x][pos_pion->y-1]==BAS || grille[pos_pion->x][pos_pion->y]==HAUT)break;
            pos_pion->y--;
            break;
    }
}
